package cn.edu.zut.salarymanagesystem.activities;

import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.io.Serializable;

import cn.edu.zut.salarymanagesystem.R;
import cn.edu.zut.salarymanagesystem.fragments.AddFragment;
import cn.edu.zut.salarymanagesystem.fragments.MainFragment;
import cn.edu.zut.salarymanagesystem.fragments.PersonFragment;
import cn.edu.zut.salarymanagesystem.fragments.QueryFragment;

public class FragmentActivity extends AppCompatActivity implements View.OnClickListener {
    private static MainFragment mainFragment = new MainFragment();
    private static AddFragment addFragment = new AddFragment();
    private static PersonFragment personalFragment = new PersonFragment();
    private static QueryFragment queryFragment = new QueryFragment();
    FragmentManager fm = getSupportFragmentManager();

    private TextView home, query, add, personal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        TurnFragment(new MainFragment());
        home = findViewById(R.id.home);
        query = findViewById(R.id.query);
        add = findViewById(R.id.add);
        personal = findViewById(R.id.personal);

        Typeface font = Typeface.createFromAsset(getAssets(), "iconfont.ttf");
        //通过home控件的setTypeface方法和iconfont.ttf文件取得联系，然后再通过setText方法将具体的icon图标设置出来
        home.setTypeface(font);
        home.setText(getResources().getString(R.string.home));
        query.setTypeface(font);
        query.setText(getResources().getString(R.string.query));
        add.setTypeface(font);
        add.setText(getResources().getString(R.string.add));
        personal.setTypeface(font);
        personal.setText(getResources().getString(R.string.personal));
        add.setOnClickListener(this);
        personal.setOnClickListener(this);
        query.setOnClickListener(this);
        home.setOnClickListener(this);
    }

    /**
     * 跳转指定的Fragment
     *
     * @param toTurn
     */
    public void TurnFragment(Fragment toTurn) {
        TurnFragment(toTurn, null);
    }

    /**
     * 携带可序列化的对象数据跳转指定的Fragment
     *
     * @param toTurn
     */
    public void TurnFragment(Fragment toTurn, Serializable object) {
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.MainFragment, toTurn);
        Bundle bun = new Bundle();
        bun.putSerializable("bun", object);
        toTurn.setArguments(bun);
        transaction.commit();
    }


    @SuppressLint({"NewApi", "ResourceType"})
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.add:
                add.setTextColor(getResources().getColor(R.color.icon_clicked));
                personal.setTextColor(getResources().getColor(R.color.icon_unclick));
                query.setTextColor(getResources().getColor(R.color.icon_unclick));
                home.setTextColor(getResources().getColor(R.color.icon_unclick));
                TurnFragment(addFragment);
                break;
            case R.id.personal:
                add.setTextColor(getResources().getColor(R.color.icon_unclick));
                personal.setTextColor(getResources().getColor(R.color.icon_clicked));
                query.setTextColor(getResources().getColor(R.color.icon_unclick));
                home.setTextColor(getResources().getColor(R.color.icon_unclick));
                TurnFragment(personalFragment);
                break;
            case R.id.query:
                add.setTextColor(getResources().getColor(R.color.icon_unclick));
                personal.setTextColor(getResources().getColor(R.color.icon_unclick));
                query.setTextColor(getResources().getColor(R.color.icon_clicked));
                home.setTextColor(getResources().getColor(R.color.icon_unclick));
                TurnFragment(queryFragment);
                break;
            case R.id.home:
                mainFragment.onCreateView(LayoutInflater.from(this),null,null);
                add.setTextColor(getResources().getColor(R.color.icon_unclick));
                personal.setTextColor(getResources().getColor(R.color.icon_unclick));
                query.setTextColor(getResources().getColor(R.color.icon_unclick));
                home.setTextColor(getResources().getColor(R.color.icon_clicked));
                TurnFragment(mainFragment);
                break;
        }

    }
}